#coding=utf-8
#Boa:Dialog:DlgAccountSubject

import wx

from product import CDataAccountSubject
from MyListCtrl import MySimpleListCtrl

def create(parent):
    return DlgAccountSubject(parent)

[wxID_DLGACCOUNTSUBJECT, wxID_DLGACCOUNTSUBJECTBTNDEL, 
 wxID_DLGACCOUNTSUBJECTBTNNEW, wxID_DLGACCOUNTSUBJECTCHECKINCOME, 
 wxID_DLGACCOUNTSUBJECTTCNAME, wxID_DLGACCOUNTSUBJECTTNAME, 
] = [wx.NewId() for _init_ctrls in range(6)]

class DlgAccountSubject(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGACCOUNTSUBJECT,
              name=u'DlgAccountSubject', parent=prnt, pos=wx.Point(424, 170),
              size=wx.Size(724, 545), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8bbe\u7f6e\u4ed8\u6b3e\u79d1\u76ee')
        self.SetClientSize(wx.Size(716, 511))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.btnNew = wx.Button(id=wxID_DLGACCOUNTSUBJECTBTNNEW,
              label=u'\u6dfb\u52a0', name=u'btnNew', parent=self,
              pos=wx.Point(368, 8), size=wx.Size(93, 26), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGACCOUNTSUBJECTBTNNEW)

        self.btnDel = wx.Button(id=wxID_DLGACCOUNTSUBJECTBTNDEL,
              label=u'\u5220\u9664\u9009\u4e2d\u9879', name=u'btnDel',
              parent=self, pos=wx.Point(496, 8), size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGACCOUNTSUBJECTBTNDEL)

        self.tcName = wx.TextCtrl(id=wxID_DLGACCOUNTSUBJECTTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(88, 8),
              size=wx.Size(208, 26), style=0, value=u'')

        self.checkIncome = wx.CheckBox(id=wxID_DLGACCOUNTSUBJECTCHECKINCOME,
              label=u'\u6536\u5165', name=u'checkIncome', parent=self,
              pos=wx.Point(304, 16), size=wx.Size(56, 16), style=0)
        self.checkIncome.SetValue(True)

        self.tName = wx.StaticText(id=wxID_DLGACCOUNTSUBJECTTNAME,
              label=u'\u65b0\u79d1\u76ee\uff1a', name=u'tName', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(68, 16), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.init_listctrl()

    def init_listctrl(self):
        
        cols = [(CDataAccountSubject.fi_id, wx.LIST_FORMAT_LEFT, -1),
                (CDataAccountSubject.fi_name, wx.LIST_FORMAT_LEFT, 150),
                (CDataAccountSubject.fi_income, wx.LIST_FORMAT_LEFT, -1)]
            
        self.lc = MySimpleListCtrl(self, CDataAccountSubject, cols)
        self.lc.Move(wx.Point(10, 42))
        self.lc.SetSize(wx.Size(550, 400))
        self.lc.Bind(wx.EVT_LEFT_DCLICK, self.OnDClick)
        self.lc.refresh()

    def getSelVal(self):
        return int(self.lc.getData(self.selRow, CDataAccountSubject.fi_id))
    
    def getSelObj(self):
        id = self.getSelVal()
        return CDataAccountSubject.findObj(id)
            
    def OnBtnNewButton(self, event):
        name = self.tcName.GetValue().strip()
        if name == '':
            return
        income = 1 if self.checkIncome.GetValue() else 0
        CDataAccountSubject.quickInsert({CDataAccountSubject.fi_name:name,
                                    CDataAccountSubject.fi_income:income})
        self.lc.refresh()

    def OnBtnDelButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        id = self.lc.getData(item, CDataAccountSubject.fi_id)
        try:
            id = int(id)
        except ValueError as error:
            print ('error id:', id)
            return
        
        obj = CDataAccountSubject.findObj(id)
        obj.delete()
        self.lc.refresh()    
        
    def OnDClick(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        self.selRow = item
        self.EndModal(wx.ID_OK)
